﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace RecruitingApplication.QuickView
{
    public partial class QuickViewDraggable : System.Web.UI.UserControl
    {
        public string CandidateName
        {
            get
            {
                return this.CandidateNameLbl.Text;
            }
            set
            {
                this.CandidateNameLbl.Text = value;
            }
        }

        public string BGColor { get; set; }

        public DateTime StaleDate { get; set; }
        public int AppID { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            //set the marker's bg color, based on stale date
            double dPercent = GetPercentageFromStaleDate(StaleDate);
            Color bgColor = GetMixedColor(Color.White, Color.Red, dPercent);
            BGColor = ColorTranslator.ToHtml(bgColor);
        }

        private double GetPercentageFromStaleDate(DateTime date)
        {
            /*/
            ***TODO: this is for debug ONLY!***
               To deploy, remove this ------------> */
            double nMinsStale = (DateTime.Now - date).TotalMinutes;

            //after two mins, we stop increasing color
            double MinsToEndOfColorIncrease = 1.0;
            double dPercent = Math.Min(nMinsStale / MinsToEndOfColorIncrease, 1);

            return dPercent;
            /*/
            double nHoursStale = (DateTime.Now - date).TotalHours;

            //do nothing until after 1 day stale
            nHoursStale = Math.Max(nHoursStale - 24, 0);

            //after two weeks, we stop increasing color
            int DaysToEndOfColorIncrease = 13;

            double dTotHrs = DaysToEndOfColorIncrease * 24;

            double dPercent = Math.Min(nHoursStale / dTotHrs, 1);

            return dPercent;
            //*/
        }

        private Color GetMixedColor(Color start, Color end, double percentage)
        {
            //get distance between colors
            int rDif = (int)end.R - (int)start.R;
            int gDif = (int)end.G - (int)start.G;
            int bDif = (int)end.B - (int)start.B;

            //shrink to percentage of total distance between colors
            rDif = (int)(rDif * percentage);
            gDif = (int)(gDif * percentage);
            bDif = (int)(bDif * percentage);

            //get the lowest of the two colors
            int rLow = Math.Min(start.R, end.R);
            int gLow = Math.Min(start.G, end.G);
            int bLow = Math.Min(start.B, end.B);

            //get the highest of the two colors
            int rHigh = Math.Max(start.R, end.R);
            int gHigh = Math.Max(start.G, end.G);
            int bHigh = Math.Max(start.B, end.B);

            //new color will be percentage between start and end
            int red = rDif + ((rDif > 0) ? rLow : rHigh);
            int green = gDif + ((gDif > 0) ? gLow : gHigh);
            int blue = bDif + ((bDif > 0) ? bLow : bHigh);

            return Color.FromArgb(red, green, blue);
        }
    }
}